package com.assignment.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.ListModel;

import com.assignment.database.DBOperations;
import com.assignment.main.Item;
import com.assignment.main.Selectable;
import com.assignment.main.Supplier;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SearchScreen extends JFrame {

	private JPanel contentPane;
	private DefaultListModel<Selectable> model;
	JList list;
	JButton btnNewFireplace,btnNewSupplier;
	final JCheckBox fireChk,stoveChk,supChk;
	SearchScreen thisFrame = this;
	
	public SearchScreen() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 555, 412);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		
		JLabel lblSelectItem = new JLabel("Search Database");
		lblSelectItem.setFont(new Font("Tahoma", Font.BOLD, 22));
		lblSelectItem.setBounds(140, 11, 223, 36);
		contentPane.add(lblSelectItem);
		
		 list = new JList();
		list.setBounds(20, 117, 489, 176);
		contentPane.add(list);
		
		 supChk = new JCheckBox("Supplier");
		supChk.setBounds(41, 64, 97, 23);
		contentPane.add(supChk);
		
		 fireChk = new JCheckBox("Fireplace");
		fireChk.setBounds(140, 64, 79, 23);
		contentPane.add(fireChk);
		
		stoveChk = new JCheckBox("Stove");
		stoveChk.setBounds(238, 64, 69, 23);
		contentPane.add(stoveChk);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setListModel(fireChk.isSelected(),stoveChk.isSelected(),supChk.isSelected());
			}
		});
		btnSearch.setBounds(323, 64, 89, 23);
		contentPane.add(btnSearch);
		
		JButton btnView = new JButton("View ");
		btnView.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Selectable s = (Selectable) list.getSelectedValue();
				s.onSelectToView(thisFrame);
				
			}
		});
		btnView.setBounds(41, 310, 89, 36);
		contentPane.add(btnView);
		
		JButton btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Selectable s = (Selectable) list.getSelectedValue();
				s.onSelectToEdit(thisFrame);
			}
		});
		btnEdit.setBounds(195, 310, 102, 36);
		contentPane.add(btnEdit);
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete?", "Delete?",  JOptionPane.YES_NO_OPTION);
				if (reply == JOptionPane.YES_OPTION)
				{
					Selectable s = (Selectable) list.getSelectedValue();
					s.onSelectToDelete();
					refreshList();
				}
				
			}
			
		});
		btnDelete.setBounds(354, 310, 97, 36);
		contentPane.add(btnDelete);
		
		 btnNewFireplace = new JButton("New Item");
		btnNewFireplace.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new EditItemScreen(thisFrame);
			}
		});
		btnNewFireplace.setBounds(420, 23, 109, 23);
		contentPane.add(btnNewFireplace);
		
		 btnNewSupplier = new JButton("New Supplier");
		btnNewSupplier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				new EditSupplierScreen(thisFrame);
			}
		});
		btnNewSupplier.setBounds(420, 64, 109, 23);
		contentPane.add(btnNewSupplier);
		
		
		setJMenuBar(new CustomJMenuBar(this));
	}
	
	
	public void setListModel(boolean fire,boolean stove,boolean supplier) {
		
		model = new DefaultListModel<Selectable>();
		
		if (fire) {
			Item[] items = DBOperations.getItems(0);
			
			for (Item i: items)
			model.addElement(i);
		}
		if (stove) {
			Item[] items = DBOperations.getItems(1);
			
			for (Item i: items)
			model.addElement(i);
		}
		if (supplier) {
			Supplier[] sups = DBOperations.getSuppliers();
			
			for (Supplier i: sups)
			model.addElement(i);
		}
		list.setModel(model);
	}
	
	public  void refreshList() {
		setListModel(fireChk.isSelected(),stoveChk.isSelected(),supChk.isSelected());
	}
}
